// 本模块为首页视图相关逻辑及状态,主要用来做页面传参
import works from '@/api/works.js'

const state = {
    worksList: [],
    worksPageTotal: 1,
    worksLastPage: 1,
    worksDetails: {},
}

const getters = {
    worksList(state) {
        return state.worksList
    },
    worksPageTotal(state) {
        return state.worksPageTotal
    },
    worksLastPage(state) {
        return state.worksLastPage
    },
    worksDetails(state) {
        return state.worksDetails
    },
}

const mutations = {
    setWorksState(state, { key, value }) {
        state[key] = value
    },
}
const actions = {
    getWorksList({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            works.works({
                per_page: data.per_page,
                page: data.page,
            }).then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setWorksState', {
                        key: 'worksList',
                        value: res.data.data.data
                    }),
                        commit('setWorksState', {
                            key: 'worksPageTotal',
                            value: res.data.data.total
                        }),
                        commit('setWorksState', {
                            key: 'worksLastPage',
                            value: res.data.data.last_page
                        })
                    resolve(res)
                }
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getWorksDetails({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            works.worksDetails(data.id).then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setWorksState', {
                        key: 'worksDetails',
                        value: res.data.data
                    })
                }
                resolve(res)
            }).catch((error) => {
                reject(error)
            });
        })
    },
}
export default {
    state,
    getters,
    actions,
    mutations
}